{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "基于 TensorFlow 构建基础的神经网络，来处理 Numpy 生成的随机数据\n",
    "\n",
    "* 构建图\n",
    "* 初始化 Session\n",
    "* 填充数据，并且获取结果\n",
    "\n",
    "> 📚 参考资料: [Intro-to-Tensorflow-DL/FirstNeuralNetwork](https://github.com/anujdutt9/Tensorflow-DeepLearning/blob/master/Intro-to-Tensorflow-DL/FirstNeuralNetwork.ipynb)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.51639863  2.51639863]\n",
      " [ 1.57066759  2.57066759]]\n"
     ]
    }
   ],
   "source": [
    "# 导入依赖\n",
    "\n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "# 设置随机数\n",
    "np.random.seed(101)\n",
    "tf.set_random_seed(101)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 51.63986277  57.06675869   2.84742265  17.15216562  68.52769817]\n",
      " [ 83.38968626  30.69662197  89.36130797  72.15438618  18.99389542]\n",
      " [ 55.42275911  35.2131954   18.18924027  78.56017619  96.54832224]\n",
      " [ 23.23536618   8.35614337  60.35484223  72.89927573  27.62388285]\n",
      " [ 68.53063288  51.78674742   4.84845374  13.78692376  18.69674261]]\n",
      "\n",
      "\n",
      "[[ 99.43179012]\n",
      " [ 52.06653967]\n",
      " [ 57.87895355]\n",
      " [ 73.48190583]\n",
      " [ 54.19617722]]\n"
     ]
    }
   ],
   "source": [
    "# 开始创建数据\n",
    "\n",
    "# 构建随机 5x5 输入矩阵\n",
    "rand_a = np.random.uniform(0,100,(5,5))\n",
    "\n",
    "print(rand_a)\n",
    "print('\\n')\n",
    "\n",
    "# 构建 5x1 输出矩阵\n",
    "rand_b = np.random.uniform(0,100,(5,1))\n",
    "\n",
    "print(rand_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 构建变量与操作\n",
    "# Placeholders require only data-type and shape (optional)\n",
    "a = tf.placeholder(tf.float32)\n",
    "b = tf.placeholder(tf.float32)\n",
    "\n",
    "# 加法操作\n",
    "add_op = a + b\n",
    "\n",
    "# 乘法操作\n",
    "mul_op = a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 151.07165527  156.49855042  102.27921295  116.58396149  167.95948792]\n",
      " [ 135.45622253   82.76316071  141.42784119  124.22093201   71.06043243]\n",
      " [ 113.30171204   93.09214783   76.06819153  136.43911743  154.42727661]\n",
      " [  96.7172699    81.83804321  133.83674622  146.38117981  101.10578918]\n",
      " [ 122.72680664  105.98292542   59.04463196   67.98310089   72.89292145]]\n",
      "\n",
      "\n",
      "[[ 5134.64404297  5674.25         283.12432861  1705.47070312\n",
      "   6813.83154297]\n",
      " [ 4341.8125      1598.26696777  4652.73388672  3756.8293457    988.9463501 ]\n",
      " [ 3207.8112793   2038.10290527  1052.77416992  4546.98046875\n",
      "   5588.11572266]\n",
      " [ 1707.37902832   614.02526855  4434.98876953  5356.77734375\n",
      "   2029.85546875]\n",
      " [ 3714.09838867  2806.64379883   262.76763916   747.19854736\n",
      "   1013.29199219]]\n"
     ]
    }
   ],
   "source": [
    "# 构建 TensorFlow Session 并且运行\n",
    "with tf.Session() as sess:\n",
    "    # Pass in values for \"a\" and \"b\" using \"feed_dict={}\"\n",
    "    add_result = sess.run(add_op,feed_dict={a:rand_a,b:rand_b})\n",
    "    print(add_result)\n",
    "    \n",
    "    print('\\n')\n",
    "    \n",
    "    multiply_result = sess.run(mul_op,feed_dict={a:rand_a,b:rand_b})\n",
    "    print(multiply_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Forward Propagation | 简单正向神经网络计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"Placeholder_5:0\", shape=(?, 10), dtype=float32)\n",
      "Tensor(\"Variable_2/read:0\", shape=(10, 3), dtype=float32)\n",
      "Tensor(\"Variable_3/read:0\", shape=(3,), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# 输入数据的特征数目\n",
    "# Number \n",
    "features_num = 10\n",
    "\n",
    "# 隐层中神经元数目\n",
    "hidden_layer_nodes_num = 3\n",
    "\n",
    "\n",
    "# 数据集\n",
    "# Placeholder# Placeh \n",
    "# x: Input Data with features = features_num\n",
    "# Rows: Num Samples\n",
    "# Cols: Num Features\n",
    "# Rows Set to None as it depends on batch size i.e. how many samples come under a batch size defined\n",
    "# X = tf.placeholder(tf.float32, shape=(rows,cols))\n",
    "X = tf.placeholder(tf.float32, shape=(None,features_num))\n",
    "\n",
    "print(X)\n",
    "\n",
    "# Variables\n",
    "# Variables need to be provided with Initial Values\n",
    "\n",
    "# Weight, 初始化随机权重\n",
    "# tf.random_normal(shape): Returns a tensor of the specified shape filled with random normal values.\n",
    "W = tf.Variable(tf.random_normal(shape=[features_num, hidden_layer_nodes_num]))\n",
    "\n",
    "print(W)\n",
    "\n",
    "# 偏移量矩阵\n",
    "# tf.ones(shape): Returns a tensor of specified shape filled with \"1's\".\n",
    "B = tf.Variable(tf.ones(shape=([hidden_layer_nodes_num])))\n",
    "\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor(\"MatMul_2:0\", shape=(?, 3), dtype=float32)\n",
      "Tensor(\"Add_2:0\", shape=(?, 3), dtype=float32)\n",
      "Tensor(\"Sigmoid_2:0\", shape=(?, 3), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "# 构建神经网络计算图\n",
    "# Multiplication Operation\n",
    "XW = tf.matmul(X,W)\n",
    "\n",
    "print(XW)\n",
    "\n",
    "\n",
    "# Addition Operation\n",
    "Z = tf.add(XW,B)\n",
    "\n",
    "print(Z)\n",
    "\n",
    "# Activation Function | 激活函数\n",
    "A = tf.sigmoid(Z)\n",
    "\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.26712373  0.82281733  0.66287428]\n",
      " [ 0.3545219   0.86869258  0.64629662]\n",
      " [ 0.37431616  0.62756479  0.69156104]\n",
      " [ 0.22459137  0.91661626  0.68012166]\n",
      " [ 0.27398425  0.75885504  0.46602228]\n",
      " [ 0.31032524  0.85040402  0.78365159]\n",
      " [ 0.42105407  0.92794871  0.39399543]\n",
      " [ 0.39582449  0.65977025  0.28165027]\n",
      " [ 0.26036012  0.86434674  0.18223025]\n",
      " [ 0.49328178  0.94590062  0.71169335]\n",
      " [ 0.32906279  0.69143319  0.22855423]\n",
      " [ 0.21358487  0.83882916  0.7003904 ]\n",
      " [ 0.3553015   0.91573012  0.7346285 ]\n",
      " [ 0.28895459  0.89422554  0.75605559]\n",
      " [ 0.47909623  0.82952327  0.54410845]\n",
      " [ 0.78835404  0.7721023   0.57869256]\n",
      " [ 0.33289137  0.83007735  0.27993578]\n",
      " [ 0.26893505  0.81677729  0.48114902]\n",
      " [ 0.29366079  0.77087712  0.41737509]]\n"
     ]
    }
   ],
   "source": [
    "# 本部分计算隐层的输出\n",
    "\n",
    "# 初始化全局变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "# 执行计算\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    \n",
    "    # Since there is only one Placeholder \"x\", provide it's value using feed_dict = {} \n",
    "    y_hat = sess.run(A, feed_dict={X:np.random.random([19,features_num])})\n",
    "    \n",
    "    # y_hat = sess.run(a,feed_dict={x:np.reshape(np.array([1,2,3,4,5,6,7,8,9,10]),[1,10])})\n",
    "    # [[  7.63079111e-09   3.67266089e-02   1.38135381e-09]]\n",
    "    # [[  1.00000000e+00   7.07776564e-07   1.00000000e+00]]  \n",
    "    \n",
    "    print(y_hat)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Simple Regression | 简单拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
